package top.humbleyuan.string;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author HumbleYuan
 * @Date 2020/5/21 19:58
 * @Des Z字变换
 * @pro 待补充找规律方法
 */
public class LeetCode_6 {
    public static void main(String[] args) {
        // 二维数组，有点蠢
        m1();

        // list存StringBuilder代表行
        m2();
    }

    private static void m1() {
        String s = "PAYPALISHIRING";
        int numRows = 3;

        /**
         设定二维数组
         **/
        char[][] res = new char[numRows][s.length()];
        //Arrays.fill(res, '0');

        int i = 0, col = 0,row;
        while(i < s.length()) {
            row = 0;
            while(row < numRows && i < s.length()) {
                res[row++][col] = s.charAt(i++);
            }

            row -= 2;
            while(row > 0 && i < s.length()) {
                res[row--][++col] = s.charAt(i++);
            }
            col++;
        }

        String newS = "";

        // 再遍历一遍
        for(int m = 0;m < res.length;m++) {
            for(int n = 0;n < res[m].length;n++) {
                if(res[m][n] != '\u0000') {
                    newS += res[m][n];
                }
            }
        }

        System.out.println(newS);
    }

    // 用list存储
    public static void m2() {
        String s = "PAYPALISHIRING";
        int numRows = 3;

        List<StringBuilder> list = new ArrayList<>();
        int minRow = Math.min(numRows, s.length());

        // 初始化
        for (int i = 0; i < minRow; i++) {
            list.add(new StringBuilder());
        }

        // 遍历s,按行归入list
        int rowIndex = 0;
        boolean goDown = false;     // 设为false主要为了第一次godown的转换
        for (int i = 0; i < s.length(); i++) {
            list.get(rowIndex).append(s.charAt(i));

            if(rowIndex == 0 || rowIndex == list.size() - 1) {
                goDown = !goDown;
            }


            rowIndex += goDown ? 1 : -1;
        }

        String res = "";
        for (StringBuilder sb : list) {
            res += sb;
        }
        System.out.println(res);
    }


    // 找规律
    public static void m3() {

    }



}
